package com.dog.basics.admin.task;

import com.dog.basics.common.properties.FileProperties;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.*;

@Slf4j
@Component
@AllArgsConstructor
public class DeamonMainTest {
	@Autowired
	private static FileProperties fileProperties;



/*

	public static void main(String[] args) throws Exception {
		DateUtil.currentSeconds();
        String f="2013-05-26 19:39:26";
		String g="2013-05-27 19:39:26";
		long a=1585620179;
		long b=1585620645;
		String e=DateUtil.formatTime(new Date(a));
		;
		String d=DateUtil.formatTime(new Date(b  * 1000L));
		long c= b-a;
		long betweenDay = DateUtil.between(DateUtil.date(b *1000L), DateUtil.date(a *1000L), DateUnit.SECOND);
		String formatBetween = DateUtil.formatBetween(DateUtil.date(b *1000L),DateUtil.date(a *1000L), BetweenFormater.Level.MILLSECOND);
		long cs = DateUtil.between(DateUtil.date(), DateUtil.endOfDay(DateUtil.date()), DateUnit.SECOND);
		System.out.println(DateUtil.formatTime(new Date( c * 1000L)));
		System.out.println(e);
		System.out.println(d);
		System.out.println(DateUtil.date(b *1000L));
		System.out.println(DateUtil.date(a *1000L));
		System.out.println(cs);
		System.out.println(NumberUtil.binaryToInt("1.1"));

	}*/

	/**
	 * Java代码实现MySQL数据库导出
	 *
	 * @author GaoHuanjie
	 * @param hostIP MySQL数据库所在服务器地址IP
	 * @param userName 进入数据库所需要的用户名
	 * @param password 进入数据库所需要的密码
	 * @param savePath 数据库导出文件保存路径
	 * @param fileName 数据库导出文件文件名
	 * @param databaseName 要导出的数据库名
	 * @return 返回true表示导出成功，否则返回false。
	 */
	public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) throws InterruptedException {

		PrintWriter printWriter = null;
		BufferedReader bufferedReader = null;
		try {
			printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));
			Process process = Runtime.getRuntime().exec(" mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName);
			InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
			bufferedReader = new BufferedReader(inputStreamReader);
			String line;
			while((line = bufferedReader.readLine())!= null){
				printWriter.println(line);
			}
			printWriter.flush();
			if(process.waitFor() == 0){//0 表示线程正常终止。
				return true;
			}
		}catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
				if (printWriter != null) {
					printWriter.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	public static void dbRestore(String root,String pwd,String dbName,String filePath){
		filePath=fileProperties.getPath() + fileProperties.getPathname()+filePath;
		StringBuilder sb = new StringBuilder();
		sb.append("mysql");
		sb.append(" -h127.0.0.1");
		sb.append(" -u"+root);
		sb.append(" -p"+pwd);
		sb.append(" "+dbName+" <");
		sb.append(filePath);
		System.out.println("cmd命令为："+sb.toString());
		Runtime runtime = Runtime.getRuntime();
		System.out.println("开始还原数据");
		try {
			Process process = runtime.exec("cmd /c"+sb.toString());
			InputStream is = process.getInputStream();
			BufferedReader bf = new BufferedReader(new InputStreamReader(is,"utf8"));
			String line = null;
			while ((line=bf.readLine())!=null){
				System.out.println(line);
			}
			is.close();
			bf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("还原成功！");
	}



	public static void main(String[] args){
/*		try {
			if (exportDatabaseTool("39.98.191.130", "root", "aPq1#3.5#78&.", "D:/clumsy-imgs/", "cessss.sql", "doctor-item")) {
				System.out.println("数据库成功备份！！！");
			} else {
				System.out.println("数据库备份失败！！！");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/
		dbRestore("root","root","doctor-item","1587031083.sql");
	}



}
